package DAO;

import java.sql.*;
import java.util.Date;

import Modele.AgentMaintenance;
import Modele.Location;
import Modele.Reparation;
import Modele.Station;
import Modele.Stationnement;
import Modele.Velo;

public class DAO {

	// Enregistrée en static comme ca on ne la charge que la première fois
	/**
	 * La connexion à la base
	 */
	private static Connection _conn;

	/**
	 * Renvoie la connexion à la base
	 * @return La connexion
	 */
	static Connection getConnection() {

		// Si la connexion n'est pas encore chargée
		if (_conn == null) {

			try {

				// Chargement du driver
				Class.forName("com.mysql.jdbc.Driver").newInstance();

			}

			catch (Exception e) {
				e.printStackTrace();
			}

			try {

				_conn = DriverManager
						.getConnection("jdbc:mysql:///nemovelo?user=root");

			} catch (SQLException e) {
				System.out.println("sql exception");
				while (e != null) {
					System.out.println(e.getErrorCode());
					System.out.println(e.getMessage());
					System.out.println(e.getSQLState());
					e.printStackTrace();
					e = e.getNextException();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return _conn;
	}

	/**
	 * Fonction de test de la base de données
	 */
	public static void testBDD() {
		try {

			System.out.println("Toutes les stations :");
			for (Station s : DAOStation.getAllStation()) {
				System.out.println("\t" + s.getIdStation() + " "
						+ s.getIntitule());
			}
			System.out.println("Station N°2: "
					+ DAOStation.getStationById(2).getIntitule());
			System.out
					.println("Ajout d'une nouvelle station en copie de la N°2.");
			DAOStation.insertStation(DAOStation.getStationById(2));
			Station newStat = DAOStation.getAllStation().get(
					(DAOStation.getAllStation().size() - 1));
			newStat.setIntitule(newStat.getIntitule() + " (Copie)");
			DAOStation.updateStation(newStat);
			System.out.println("\nToutes les stations :");
			for (Station s : DAOStation.getAllStation()) {
				System.out.println("\t" + s.getIdStation() + " "
						+ s.getIntitule());
			}
			System.out.println("Suppression de la nouvelle station.");
			DAOStation.deleteStation(DAOStation.getAllStation().get(
					(DAOStation.getAllStation().size() - 1)));
			System.out.println("\nToutes les stations :");
			for (Station s : DAOStation.getAllStation()) {
				System.out.println("\t" + s.getIdStation() + " "
						+ s.getIntitule());
			}

			System.out.println("\nTous les vélos :");
			for (Velo v : DAOVelo.getAllVelo()) {
				System.out.println("\t" + v.getIdVelo() + "  "
						+ v.getDateAchat());
			}
			System.out.println("Velo N°2: "
					+ DAOVelo.getVeloById(2).getIdVelo() + " "
					+ DAOVelo.getVeloById(2).getDateAchat());

			System.out.println("\nTous les agents de maintenance :");
			for (AgentMaintenance a : DAOAgentMaintenance
					.getAllAgentMaintenance()) {
				System.out.println("\t" + a.getIdAgent() + "  " + a.getNom()
						+ " " + a.getPrenom() + " " + a.getDateDeNaissance()
						+ " " + a.getLieuDeNaissance());
			}
			System.out.println("Agent N°2: "
					+ DAOAgentMaintenance.getAgentMaintenanceById(2).getNom()
					+ " "
					+ DAOAgentMaintenance.getAgentMaintenanceById(2)
							.getPrenom());

			System.out.println("\nToutes les locations");
			for (Location loc : DAOLocation.getAllLocation()) {
				System.out.println("\tClient: " + loc.getClient().getNom()
						+ " " + loc.getClient().getPrenom() + "     velo: "
						+ loc.getVelo().getIdVelo()
						+ "        Station de depart :"
						+ loc.getStationDebut().getIntitule());
			}

			System.out.println("\nTous les stationnements");
			for (Stationnement stat : DAOStationnement.getAllStationnement()) {
				System.out.println("\tStation: "
						+ stat.getStation().getIntitule() + "   Velo: "
						+ stat.getVelo().getIdVelo() + "   Date de début: "
						+ stat.getDateDeDebut());
			}

			System.out.println("\nToutes les reparations");
			for (Reparation rep : DAOReparation.getAllReparation()) {
				System.out.println("\tAgent: "
						+ rep.getAgentMaintenance().getNom() + "   Velo: "
						+ rep.getVelo().getIdVelo() + "   Date de réparation: "
						+ rep.getDateDeReparation());
			}

			System.out.println("\nAjout d'une location sans arrivée.");
			DAOLocation.insertLocation(new Location(1, 2, 2, 0, new Date(),
					null));
			System.out.println("\nToutes les locations");
			for (Location loc : DAOLocation.getAllLocation()) {
				System.out.println("\tClient: " + loc.getClient().getNom()
						+ " " + loc.getClient().getPrenom() + "     velo: "
						+ loc.getVelo().getIdVelo()
						+ "        Station de depart :"
						+ loc.getStationDebut().getIntitule());
			}

			System.out.println("\nFin du test BDD ...");

		} catch (Exception ex) {
			System.out.println("Exception: " + ex.getMessage());
			ex.printStackTrace();
		}
	}

}
